import {
  resourceUrl,
  randomCode,
  apiMode
} from '../../../utils/env.js';
import{
  drawFitableCoupon,
  getConsumerCoupons,
  getOfferAssistCount,
  getCurrentMemberInfo,
  getOfferShareAssist
} from '../../../utils/api'

// pages/graduationSeason/graduationSeason.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    beAssistUserId:[523,524,525],
    couponList: [
      { OfferID: 516, OfferName: "2021毕业季", Comments: "毕业季--20元无门槛优惠券" }
    ],
    userImg1:'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/18.png',
    userImg2:'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/color.png',
    OfferID: 523,
    OfferName:"2021毕业季",
    imgUrls:[
        'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/15.png',
        'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/16.png',
        'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/17.png',
    ],
    swiperIndex: 0,
    myUserID:0,
    _name:'',
    resourceUrl:resourceUrl,
    randomCode:randomCode,
    noticeShow:false,
    circular: true,
    autoplay: true,
    progress:0,
    helpNumber: 0,
    Name:'',//用户名称
    Telephone:""//用户电话
  },
  onShow() {
    this.initData();
  },
  onShareAppMessage () {
    let _telephone = this.data.Telephone
    // let _name= this.data
    console.log(_telephone)
    return {
      title: '看在多年好友的份上 亲，帮助力一次吧',
      imageUrl: "https://cms.dafang24.com/MicroAppResource/images/assistance_2021/fx_01.png",
      path:`pages/rent/assistanceShare/assistanceShare?userID=${this.data.myUserID}&Name=${this.data._name}`
    }
  },
  async getOfferAssistCountFn (){
    console.log(this.data.beAssistUserId);
    let resd = await getOfferAssistCount({
      data: this.data.beAssistUserId
    });
    if(resd.Code==100){
    let _helpNumber = (resd.Data || 0)*1;
    this.setData({
     helpNumber: _helpNumber 
    })
    let _progress = 0 
    if(_helpNumber >= 2){
      _progress = 132
    };
    if(_helpNumber>= 4){
      _progress = 346
    }
    if(_helpNumber>=8){
      _progress = 543
    }
    this.setData({
      progress:_progress
    });
    };
    // console.log(this.data.helpNumber)
  },
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  async checkoutOwnerCoupon() {
    let res = await getConsumerCoupons();
    console.log(res)
    if (res.Code == 100) {
      if (res.Data && res.Data.length) {
        let _couponList = this.data.couponList;
        for (let i in res.Data) {
          for (let j = 0; j < _couponList.length; j++) {
            if (res.Data[i].OfferID == _couponList[j].OfferID) {
              _couponList.splice(j, 1);
            }
          }
        }
        this.setData({ couponList: _couponList })
      }
    }
  },
  async initData() {
    if ( apiMode!='build' ){
      this.setData({
        assistanceID: apiMode == 'dev' ? 516 : 516
      });
    }
    this.getUserInfo();
  },
  async getUserInfo () {
    if (this.data.myUserID>0 ){ return; }
    let res = await getCurrentMemberInfo();
    if( res.Code==100 ){
      this.setData({ 
        myUserID: res.Data.UserID,
        _name:res.Data.Name
      });
      // console.log(res.Data.Name,this.data._name)
      this.getOfferAssistCountFn();
      this.checkoutOwnerCoupon();
    }
    // console.log(res)
  },
  // 领取优惠券
  async getCoupon() {
    let _couponList = this.data.couponList;
    if (_couponList.length) {
      let getNum = 0, errStr = '';
      for (let i in _couponList ){
        let res = await drawFitableCoupon({
          data: { offerID: _couponList[i].OfferID, orgID: 0 }
        });
        console.log(res,'_____')
        if (res.Code == 100) {
          getNum++;
        } else {
          errStr = res.Message;
        }
      }
      if (getNum > 0) {
        wx.showToast({
          title: "领取成功！",
          icon: 'none',
          duration: 1000
        });
        this.checkoutOwnerCoupon();
        return;
      }
      wx.showToast({
        title: errStr,
        icon: 'none',
        duration: 2000
      });
    } else {
      wx.showModal({
        title: '已经领过券',
        content: '已经领过券了，立即前往租车啦~',
        cancelText: '再看看',
        confirmText: '立即租车',
        success:(res)=>{
          if (res.confirm) {
            wx.switchTab({ url: '/pages/rent/shortrent/shortrent' })
          }
        }
      })
    }
  },
  onShareFn() {
    // console.log('++++++++')
    // if (this.data.helpNumber >= 8) {
    //   wx.showModal({
    //     title: '已领券',
    //     content: '已经领到券了，立即前往租车啦~',
    //     confirmText: '立即租车',
    //     cancelText: '再看看',
    //     success: (result) => {
    //       if (result.confirm) {
    //         wx.switchTab({ url: '/pages/rent/shortrent/shortrent' });
    //       }
    //     },
    //   });
    // }
  },
  goHome() {
    wx.switchTab({
      url: '/pages/rent/shortrent/shortrent'
    });
  },
  showNotice (){
    let _noticeShow = this.data.noticeShow;
    this.setData({noticeShow:!_noticeShow})
  },
})

